using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

using System.Collections.Generic;
 

public class CameraKeyworldFeature : ScriptableRendererFeature
{
    [System.Serializable]
    public struct CameraKeywordSetting
    {
        public string [] cameraNames;
    
        public string [] keywords;
    }
    [SerializeField]
    private List<CameraKeywordSetting> cameraKeywordSettings = new List<CameraKeywordSetting>();

    class KeywordPass : ScriptableRenderPass
    {
        private List<CameraKeywordSetting> settings;

        public KeywordPass(List<CameraKeywordSetting> settings)
        {
            this.settings = settings;
            renderPassEvent = RenderPassEvent.BeforeRendering;
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            Camera cam = renderingData.cameraData.camera;

            
            for (int i = 0,count = settings.Count; i < count; i++)
            {
                var setting = settings[i];
                bool found = false;
                for (int k = 0, countk = setting.cameraNames.Length; k < countk; k++)
                {
                    if (cam.name == setting.cameraNames[k])
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    for (int j = 0, countj = setting.keywords.Length; j < countj; j++)
                    {
                        Shader.EnableKeyword(setting.keywords[j]);
                    }
                }
                else
                {
                    for (int j = 0, countj = setting.keywords.Length; j < countj; j++)
                    {
                        Shader.DisableKeyword(setting.keywords[j]);
                    }
                }
                
                
            }
            
        }
    }

    private KeywordPass keywordPass;

    public override void Create()
    {
        keywordPass = new KeywordPass(cameraKeywordSettings);
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(keywordPass);
    }
}
 